//
//  XQHomeSmallCardView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQHomeSmallCardView.h"
#import "XQHomeSmallCardCell.h"

@interface XQHomeSmallCardView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation XQHomeSmallCardView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
        make.height.equalTo(@0);
    }];
}

#pragma mark - setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    
    [self.tableView reloadData];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(111 * dataSource.count);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    XQHomeSmallCardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQHomeSmallCardCell" forIndexPath:indexPath];
    [cell setContentDic:dic];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 111;;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.row];
    id cardId = dic[@"viasat"];

    if (self.didSelectIndex) {
        self.didSelectIndex([cardId description]);
    }
}

#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[XQHomeSmallCardCell class] forCellReuseIdentifier:@"XQHomeSmallCardCell"];
    }
    return _tableView;
}
@end
